package com.techshine.util;

import java.io.PrintStream;

public class BytePackage
{
  public static void main(String[] args)
    throws Exception
  {
    Integer valueOf = Integer.valueOf("4E", 16);
  }
  
  public static void setHead(byte[] arr)
  {
    arr[0] = -52;
    arr[1] = -18;
  }
  
  public static void setLen(byte[] arr, int len)
  {
    arr[3] = ((byte)(len >> 8 & 0xFF));
    arr[2] = ((byte)len);
  }
  
  public static void setpanId(byte[] arr, int addr)
  {
    arr[5] = ((byte)(addr >> 8 & 0xFF));
    arr[4] = ((byte)addr);
  }
  
  public static void setradius(byte[] arr, int radius)
  {
    arr[6] = ((byte)radius);
  }
  
  public static void setrssi(byte[] arr, int rssi)
  {
    arr[7] = ((byte)rssi);
  }
  
  public static void setafAddrMode_t(byte[] arr, int afAddrMode)
  {
    arr[8] = ((byte)afAddrMode);
  }
  
  public static void setgoupId(byte[] arr, int goupId)
  {
    arr[9] = ((byte)goupId);
  }
  
  public static void setendaddr(byte[] arr, int nodeaddr)
  {
    arr[11] = ((byte)(nodeaddr >> 8 & 0xFF));
    arr[10] = ((byte)nodeaddr);
  }
  
  public static void setendtype(byte[] arr, int nodetype)
  {
    arr[13] = ((byte)(nodetype >> 8 & 0xFF));
    arr[12] = ((byte)nodetype);
  }
  
  public static void setcomm(byte[] arr, int comm)
  {
    arr[14] = ((byte)comm);
  }
  
  public static void setdata(byte[] arr, byte[] body)
  {
    System.arraycopy(body, 0, arr, 15, body.length);
  }
  
  public static void setcheck(byte[] arr)
  {
    int len = arr.length;
    
    arr[(len - 1)] = 0;
    int n = 0;
    for (int i = 0; i < arr.length; i++) {
      n += arr[i];
    }
    arr[(arr.length - 1)] = ((byte)n);
  }
  
  public static int getShort(byte[] arr, int index)
  {
    byte byte0 = arr[index];
    byte byte1 = arr[(index + 1)];
    return (byte1 & 0xFF) << 8 | byte0 & 0xFF;
  }
  
  public static void setBody(byte[] all, int index, byte[] body)
  {
    System.arraycopy(body, 0, all, index, body.length);
  }
  
  public static void setCheck(byte[] all)
  {
    int n = 0;
    for (int i = 0; i < all.length; i++) {
      n ^= all[i];
    }
    all[(all.length - 1)] = ((byte)n);
  }
  
  public static boolean checkSum(byte[] all)
  {
    int n = 0;
    for (int i = 0; i < all.length; i++) {
      n ^= all[i];
    }
    return all[(all.length - 1)] == n;
  }
  
  public static byte[] intToByte(int num)
  {
    byte[] b = new byte[4];
    b[0] = ((byte)(num >> 24 & 0xFF));
    b[1] = ((byte)(num >> 16 & 0xFF));
    b[2] = ((byte)(num >> 8 & 0xFF));
    b[3] = ((byte)num);
    return b;
  }
  
  public static void myLog(byte[] all, String msg)
  {
    System.out.print(msg + ":[");
    byte[] arrayOfByte = all;int j = all.length;
    for (int i = 0; i < j; i++)
    {
      byte b = arrayOfByte[i];
      String tmp = Integer.toHexString(0xFF & b);
      if (tmp.length() == 1) {
        tmp = "0" + tmp;
      }
      System.out.print(tmp + " ");
    }
    System.out.println("]");
  }
  
  private static int parse(char c)
  {
    if (c >= 'a') {
      return c - 'a' + 10 & 0xF;
    }
    if (c >= 'A') {
      return c - 'A' + 10 & 0xF;
    }
    return c - '0' & 0xF;
  }
  
  public static byte[] hexString2Bytes(String hexstr)
  {
    byte[] b = new byte[hexstr.length() / 2];
    int j = 0;
    for (int i = 0; i < b.length; i++)
    {
      char c0 = hexstr.charAt(j++);
      char c1 = hexstr.charAt(j++);
      b[i] = ((byte)(parse(c0) << 4 | parse(c1)));
    }
    return b;
  }
  
  public static String bytesToHex(byte[] inBytArr)
  {
    StringBuilder strBuilder = new StringBuilder();
    int j = inBytArr.length;
    for (int i = 0; i < j; i++)
    {
      strBuilder.append(byte2Hex(Byte.valueOf(inBytArr[i])));
      strBuilder.append(" ");
    }
    return strBuilder.toString();
  }
  
  public static String byte2Hex(Byte inByte)
  {
    return String.format("%02x", new Object[] { inByte }).toUpperCase();
  }
}
